package com.shouhuobao.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.logic.MsgID;
import com.shouhuobao.bhi.loc.BaiduLocationService;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;

/**
 * 快捷寄件通道
 * Created by lidandan on 2018/5/16.
 */
public class CallCenterOrderActivity extends BaseActivity2 {

    private String currentPositionStr = "<font color=#848c97>当前位置：</font><font color=#404040><small>{0}</small></font>";
    private String mouthStr = "<font color=#404040>我需要寄一个</font>" +
            "<font color=#828C99><u> 衣服</u></font><font color=#F7622E>（物品）</font>" +
            "<font color=#404040>，请火箭侠在</font><font color=#828C99><u> 15:30</u></font>" +
            "<font color=#F7622E>（时间）</font><font color=#404040>来取件，位置在：</font>" +
            "<font color=#828C99><u> 某街道某小区几单元几</u></font>" +
            "<font color=#F7622E>（地点）</font>";
    private TextView mCurrentPositionTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog("正在定位");
        Intent intent = new Intent();
        intent.setClass(getContext(), BaiduLocationService.class);
        intent.setPackage(getPackageName());
        intent.putExtra("msgId", MsgID.self_baidu_location);
        startService(intent);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.call_center_order_activity);
        super.findViewById();
        getAppTitle().setCommonTitle("快捷寄件通道");
        //话术
        TextView mContentTV = (TextView) findViewById(R.id.content_tv);
        mContentTV.setText(Html.fromHtml(mouthStr));
        //呼叫客服
        LinearLayout mCallCenterLL = (LinearLayout) findViewById(R.id.call_center_ll);
        mCallCenterLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simpleDate();
            }
        });
        //当前位置
        mCurrentPositionTV = (TextView) findViewById(R.id.current_position_tv);


    }

    @Override
    public boolean handleMessage(final Message msg) {
        switch (msg.what) {
            //定位回调方法
            case MsgID.self_baidu_location:
                cancelLoadingDialog();
                final BDLocation bdLocation = (BDLocation) msg.obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (bdLocation != null && bdLocation.getPoiList() != null && bdLocation.getPoiList().size() > 0) {
                            mCurrentPositionTV.setText(Html.fromHtml(MessageFormat.format(currentPositionStr, bdLocation.getPoiList().get(0).getName())));
                        } else {
                            showToast("定位失败");
                        }
                    }
                });

                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    /**
     * 获取当时的时间
     */
    public void simpleDate() {
        SimpleDateFormat format24 = new SimpleDateFormat("HH");
        String str = format24.format(new java.util.Date());
        int hour = Integer.parseInt(str);
        int start = 15;// 起始时间0点
        int end = 20;// 下午时间
        if (hour >= start && hour < end) {
            DialogMgr.callCenter(getContext());
        } else {
            DialogMgr.notCallCenter(getContext());
        }
    }
}
